import axios from '../index'

/**
 * Edge Tab API管理器
 * 处理Edge Tab相关的CefSharp调用和JSON配置管理
 */
class EdgeTabManager {
    constructor() {
        this.baseUrl = '/edge'
    }

    // ==================== MqttFormatJson相关 ====================//

    // 添加mqtt-format-json配置
    async addMqttFormatJson(data) {
        console.log('🔍 [EdgeTabManager] 添加Tab', { data })
        try {
            const response = await axios.post(`${this.baseUrl}/mqtt-format-json/add`, data)
            console.log('✅ [EdgeTabManager] 添加Tab成功', { data })
            return response
        } catch (error) {
            console.error('❌ [EdgeTabManager] 添加Tab失败:', error)
            throw error
        }
    }

    // 删除mqtt-format-json配置
    async deleteMqttFormatJson(data) {
        console.log('🔍 [EdgeTabManager] 删除Tab', { data })
        try {
            const response = await axios.post(`${this.baseUrl}/mqtt-format-json/delete`, data)
            console.log('✅ [EdgeTabManager] 删除Tab成功', { data })
            return response
        } catch (error) {
            console.error('❌ [EdgeTabManager] 删除Tab失败:', error)
            throw error
        }
    }

    // 保存mqtt-format-json配置
    async saveMqttFormatJson(configData) {
        console.log('💾 [EdgeTabManager] 保存Tab配置')
        try {
            const response = await axios.post(`${this.baseUrl}/mqtt-format-json/save`, { configData })
            console.log('✅ [EdgeTabManager] 保存Tab配置成功')
            return response
        } catch (error) {
            console.error('❌ [EdgeTabManager] 保存Tab配置失败:', error)
            throw error
        }
    }

    // 获取mqtt-format-json所有配置
    async getMqttFormatJsonAll() {
        console.log('🔍 [EdgeTabManager] 获取mqtt-format-json所有配置')
        try {
            const response = await axios.get(`${this.baseUrl}/mqtt-format-json/all`)
            console.log('✅ [EdgeTabManager] 获取mqtt-format-json所有配置成功')
            return response
        } catch (error) {
            console.error('❌ [EdgeTabManager] 获取mqtt-format-json所有配置失败:', error)
            throw error
        }
    }

    /**
     * 获取设备树数据
     * @param {string} param - 设备树查询参数
     * @returns {Promise<string>} 设备树JSON字符串
     */
    async getMqttFormatJsonDeviceTree(param = '') {
        console.log('🔍 [EdgeTabManager] 获取设备树数据', { param })
        try {
            const response = await axios.get(`${this.baseUrl}/mqtt-format-json/device-tree`)
            console.log('✅ [EdgeTabManager] 获取设备树数据成功')
            return response || JSON.stringify([])
        } catch (error) {
            console.error('❌ [EdgeTabManager] 获取设备树数据失败:', error)
            return JSON.stringify([])
        }
    }

    /**
     * 获取mqtt-format-json设备变量JSON数据
     * @param {string} deviceId - 设备ID
     * @returns {Promise<string>} 设备变量JSON字符串
     */
    async getMqttFormatJsonDevicesVarJson(deviceId) {
        console.log('🔍 [EdgeTabManager] 获取设备变量JSON数据', { deviceId })
        try {
            const response = await axios.get(`${this.baseUrl}/mqtt-format-json/devices/${deviceId}/variables`)
            console.log('✅ [EdgeTabManager] 获取设备变量JSON数据成功', { deviceId })
            return response || JSON.stringify({ labels: [] })
        } catch (error) {
            console.error('❌ [EdgeTabManager] 获取设备变量JSON数据失败:', error)
            return JSON.stringify({ labels: [] })
        }
    }

    async runLuaScript(code) {
        console.log('🔍 [EdgeTabManager] 运行Lua脚本', { code })
        try {
            const response = await axios.post(`${this.baseUrl}/lua/run`, code)
            console.log('✅ [EdgeTabManager] 运行Lua脚本成功', { code })
            return response
        } catch (error) {
            console.error('❌ [EdgeTabManager] 运行Lua脚本失败:', error)
            throw error
        }
    }

    async saveLuaScript(code) {
        console.log('🔍 [EdgeTabManager] 保存Lua脚本', { code })
        try {
            const response = await axios.post(`${this.baseUrl}/lua/save`, code)
            console.log('✅ [EdgeTabManager] 保存Lua脚本成功', { code })
            return response
        } catch (error) {
            console.error('❌ [EdgeTabManager] 保存Lua脚本失败:', error)
            throw error
        }
    }

    async getLuaScript(keyName) {
        console.log('🔍 [EdgeTabManager] 获取Lua脚本', { keyName })
        try {
            const response = await axios.get(`${this.baseUrl}/lua/get`, {
                params: { key: keyName }
            })
            console.log('✅ [EdgeTabManager] 获取Lua脚本成功', { keyName })
            return response
        } catch (error) {
            console.error('❌ [EdgeTabManager] 获取Lua脚本失败:', error)
            throw error
        }
    }
}

export default EdgeTabManager